package com.it.gw.modules.station.adapter

import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.it.data.network.rsp.QuaTypeCountVo
import com.it.gw.databinding.ItemQualTypeCountBinding


class QualTypeCountListAdapter(
    var context: Context,
    var listener: (QuaTypeCountVo) -> Unit,
    var datas: List<QuaTypeCountVo>
) : RecyclerView.Adapter<QualTypeCountListAdapter.ViewHolder>() {
    companion object {
        const val TAG = "GQStationListAdapter"
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): ViewHolder {
        val binding = ItemQualTypeCountBinding.inflate(LayoutInflater.from(parent.context))
        return ViewHolder(binding)
    }


    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val data = datas[position]
        Log.d(TAG, "onBindViewHolder: $data")
        holder.binding.one.text = data.DATA_NAME
        holder.binding.two.text = "${data.count}"
        val percentage = data.count.toDouble() / data.totalCount * 100
        holder.binding.three.text = String.format("%.2f%%", percentage)

        holder.binding.root.setOnClickListener {
            listener.invoke(data)
        }

    }


    override fun getItemCount() = datas.size

    class ViewHolder(val binding: ItemQualTypeCountBinding) : RecyclerView.ViewHolder(binding.root)
}